package com.example.dialog.Activity;

import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.dialog.Activity.HomeActivity;
import com.example.dialog.R;
import com.example.dialog.Tools.FileSaveInfo;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;

public class MainActivity extends AppCompatActivity {

    private EditText edtUsername, edtPassword;
    private Button btnLogin;
    private TextView tvRegister;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        edtUsername = findViewById(R.id.edtUsername);
        edtPassword = findViewById(R.id.edtPassword);
        btnLogin = findViewById(R.id.btnLogin);
        tvRegister = findViewById(R.id.tvRegister);

        Map<String, String> userInfo = FileSaveInfo.getUserInfo(this);
        if (userInfo != null) { //加载上次的账号密码
            edtUsername.setText(userInfo.get("account"));
            edtPassword.setText(userInfo.get("password"));
        }

        btnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 获取用户名和密码
                String username = edtUsername.getText().toString().trim();
                String password = edtPassword.getText().toString().trim();


                // 简单验证输入内容
                if (username.isEmpty() || password.isEmpty()) {
                    Toast.makeText(MainActivity.this, "用户名或密码不能为空", Toast.LENGTH_SHORT).show();
                } else {
                    // 获取之前保存的用户名和密码
                    Map<String, String> savedUserInfo = FileSaveInfo.getUserInfo(MainActivity.this);
                    if (savedUserInfo != null) {
                        boolean is_exist = new DatabaseHelperForCheckUsr(MainActivity.this).check_usr(edtUsername.getText().toString(), edtPassword.getText().toString());
                        // 验证用户名和密码
                        if (is_exist) {
                            // 登录成功
                            Toast.makeText(MainActivity.this, "登陆成功", Toast.LENGTH_SHORT).show();
                            Intent intent=new Intent(MainActivity.this, HomeActivity.class);
                            startActivity(intent);
                        } else {
                            // 登录失败
                            Toast.makeText(MainActivity.this, "账号或密码错误!", Toast.LENGTH_SHORT).show();
                        }
                    } else {

                        Toast.makeText(MainActivity.this, "未找到该账号", Toast.LENGTH_SHORT).show();
                    }

                    // 保存用户名和密码
                    FileSaveInfo.saveUserInfo(MainActivity.this, username, password);
                }
            }
        });


        tvRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(MainActivity.this, RegisterActivity.class);
                startActivity(intent);

            }
        });


    }//onCreate
    class DatabaseHelperForCheckUsr extends SQLiteOpenHelper{
        private static final String DATABASE_NAME = "searchdatabase.db";
        private static final int DATABASE_VERSION = 1;
        private static final String TABLE_NAME = "usrinfo";
        private static final String COL_1 = "usrAct"; // 账号
        private static final String COL_2 = "usrPwd"; // 密码

        public DatabaseHelperForCheckUsr(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase sqLiteDatabase) {

        }

        @Override
        public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

        }
        public boolean check_usr(String usrAct, String usrPwd) {
            SQLiteDatabase db = this.getReadableDatabase();
            Cursor cursor = db.query(TABLE_NAME, null, COL_1 + "=? AND " + COL_2 + "=?",
                    new String[]{usrAct, usrPwd}, null, null, null);

            boolean exists = cursor.getCount() > 0;
            cursor.close();
            db.close();
            return exists;
        }
    }
}